#include <QFile>
#include <QPushButton>

#include "constants.h"
#include "contextmanager.h"
#include "icore.h"
#include "mainwindow.h"
#include "sidecontacts.h"
#include "uidmanager.h"

SideContacts::SideContacts(QWidget * parent) :
    ContactListView(parent),
    m_allContactsButton(new QPushButton(this))
{
    setSpacing(4);
    setFixedHeight(185);
    setViewportMargins(0, 0, 0, 26);

    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    setDisplayMode(ContactDelegate::SmallPicture);

    QFile f(":/core/images/allcontacts.qss");
    f.open(QIODevice::ReadOnly);
    setStyleSheet(f.readAll());

    m_allContactsButton->setText(tr("All contacts"));
    m_allContactsButton->setFixedHeight(25);
    connect(m_allContactsButton, SIGNAL(released()), this, SLOT(showAllContacts()));
    connect(this, SIGNAL(messagingRequest(ContactInfo)),
            ICore::instance()->mainWindow(), SLOT(openMessaging(ContactInfo)));
}

void SideContacts::resizeEvent(QResizeEvent *e)
{
    ContactListView::resizeEvent(e);

    m_allContactsButton->move(1, height() - m_allContactsButton->height() - 1);
    m_allContactsButton->resize(width() - 2, m_allContactsButton->height());
}

void SideContacts::showAllContacts()
{
    ContextManager * cm = ICore::instance()->contextManager();
    UidManager * uidManager = ICore::instance()->uidManager();

    cm->setGuiContext(uidManager->uniqueIndentifier(Constants::C_CONTACTS));
}
